package ${servicePackage}.impl;

import co.baiku.boot.common.tools.ObjectTools;
import ${poPackage}.${className};
import ${dtoPackage}.${className}Dto;
import ${daoPackage}.${className}DAO;
import ${servicePackage}.${className}Service;
import co.baiku.boot.common.exception.ApiException;
import co.baiku.boot.common.validate.Validation;
import co.baiku.boot.core.orm.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.ArrayList;
import java.util.List;

/**
 * ${classComment}
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ${className}ServiceImpl extends BaseServiceImpl<${className}, ${className}Dto, ${pkType}> implements ${className}Service {

    private ${className}DAO ${className?uncap_first}DAO;

    @Autowired
    public ${className}ServiceImpl(${className}DAO ${className?uncap_first}DAO) {
        super(${className?uncap_first}DAO);
        this.${className?uncap_first}DAO = ${className?uncap_first}DAO;
    }

    @Override
    public ${className}Dto save${className}(${className}Dto ${className?uncap_first}) {
        if (ObjectTools.isBlank(${className?uncap_first})) {
            throw ApiException.of("要保存的对象不能为空");
        }
        Validation.assertSuccess(${className?uncap_first});
        if (ObjectTools.isNotBlank(${className?uncap_first}.getId())) {
            //更新
            ${className} db${className} = getById(${className?uncap_first}.getId());
            if (db${className} == null) {
                throw ApiException.of("对应数据不存在");
            }
        }
        return toDto(saveOrUpdate(toPo(${className?uncap_first})));
    }

    @Override
    public List<${className}Dto> findDtoByIdIn(Collection<${pkType}> ids) {
        if (ObjectTools.isNotBlank(ids)) {
            return toDto(${className?uncap_first}DAO.findByIdIn(ids));
        } else {
            return new ArrayList<>();
        }
    }
}